
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.Text;

using Swaf;
using Swaf.BizRule;
using Swaf.BizObj;
using Swaf.Container;
using Swaf.Gui;
using Swaf.Security;
using Syrinx.Gui;

namespace Contacts.Gui
{
	public class ContactSignUpBP : ContactManagementBP
	{
		protected bool m_inRememberUpdate = false;
		public ContactSignUpBP(ActiveBizProc procState) : base(procState)
		{
			//if (State.InProgressAddBizObj == null)
			//{
			//    State.InProgressAddBizObj = createNewBizObj();
			//    updateActionState();
			//}
		}

		protected override void setupRuleNames()
		{
			base.setupRuleNames();
			base.m_getBizObjListRule = null;
		}

		protected override void getCurrentBizObj()
		{
			base.getCurrentBizObj();
			if(m_currentBizObj == null)
				State.InProgressAddBizObj = m_currentBizObj = createNewBizObj();
			rememberCurrentDisplayValues();
			IBizObj order = m_procState.get("orderInfo", false) as IBizObj;
			if (m_currentBizObj != null && order != null)
			{
				//if (((IComparable)m_currentBizObj.get("id")).CompareTo(order.get("orderContact.id")) != 0)
				//{
				//    m_currentBizObj.put("id", new Swaf.BGuid.Guid());
				//    m_currentBizObj.isNew = true;
				//}
				order.put("orderContact", m_currentBizObj);
			}
		}

		protected virtual void rememberCurrentDisplayValues()
		{
			m_inRememberUpdate = true;
			IBizObj cur = m_currentBizObj;
			if(!(cur.isFieldSet("firstName") || cur.isFieldSet("lastName") || cur.isFieldSet("addresses.[0].zipCode")))
				updateBizObj();
			cur.isNew = false;
			foreach (IBizObj adr in (IBizObjList)cur.get("addresses"))
				adr.isNew = false;
			m_inRememberUpdate = false;
		}

		protected override object[] getDetailsBizRuleParameters(object keyValue)
		{
			IBizObj order = m_procState.get("orderInfo", false) as IBizObj;
			if (order != null)
				keyValue = order.get("orderContact.id");
			if(keyValue == null && true.Equals(CurrentUser.get("authenticated")))
				keyValue = CurrentUser.get("id");
			return base.getDetailsBizRuleParameters(keyValue);
		}
		
		public override Swaf.IResults updateBizObj(Swaf.BizObj.IBizObj obj)
		{
			IResults rc = base.updateBizObj(obj);

			if (m_inRememberUpdate)
				return rc;// base.updateBizObj(obj);

			
			IBizObj wizardInfo = m_procState.get("info",false) as IBizObj;
			if( requiredFieldMissing(new string[]{"lastName","firstName","email","phone"},obj))
			{
				if (wizardInfo != null)
					wizardInfo.put("stepCompleted", false);
				return rc;
			}
			//rc= base.updateBizObj(obj);
			if (rc.ExceptionInfo == null)
			{
				IBizObj order = m_procState.get("orderInfo", false) as IBizObj;
				if (order!=null && wizardInfo!=null)
				{
					IBizObj contact = rc.CallResults as IBizObj;
					order.put("orderContact", contact);
					if(!wizardInfo.isDirty("stepCompleted"))
						wizardInfo.put("stepCompleted", true);
				}
			}
			return rc;						
		}
		public override void updateActionState()
		{
			base.updateActionState();
			IBizObj order = m_procState.get("orderInfo") as IBizObj;
			//TODO: needs to include check for the order status. 
			//		we dont want checkout of order that is in progress
			if (order == null || (int)order.get("itemCount") == 0)
				((GenericAction)Actions["MoveNext"]).Availability = ActionAvailability.none;
		}

		public override void clearRetrievedData()
		{
			//Do nothing in this case so we dont lose data provided by user already.
			//base.clearRetrievedData();

		}

		public override IBizObj createNewBizObj()
		{
			IBizObj obj = base.createNewBizObj();
			if (!Cmn.IsEmpty(ActiveCompany.get("settings.defaultNewContactCountry")))
				obj.put("defaultAddress.Country.regionCode", ActiveCompany.get("settings.defaultNewContactCountry"));
			return obj;
		}
		public override Privileges CurrentEditPrivilege{get {return Privileges.Add;}}
		
	}
}
